import LTRes from "../Res/LTRes";
import ResUrl from "../Res/ResUrl";
import Scene, { node, Prefab } from "./Scene";
import CommonSaveData from "../Commom/CommonSaveData";
import MesManager from "../Manager/MesManager";
import { EEventScene } from "../../script/enum/EEventScene";
import { LevelConfig } from "../../script/config/LevelConfig";

// 场景3d  可取出关卡
export default class SceneManager {
    public static get instance(): SceneManager {
        if (this._instance == null) {
            this._instance = new SceneManager();
        }
        return this._instance;
    }

    private static _instance: SceneManager;
    private _levelConfig: {} = {};
    private _scenes: Scene[] = [];

    private constructor() { }

    public initConfig() {
        let url = ResUrl.config_url();
        let config = LTRes.Get(url, true);
        if (config.root) {
            for (let i = 0; i < config.root.length; i++) {
                let data = config.root[i];
                this._levelConfig[data.name] = data;
            }
        }
        Laya.loader.clearRes(url);
    }

    public initCamera(camera: Laya.Camera) {
        let cameraData = this._levelConfig["camera"];
        if (cameraData) {
            this.initNode(camera, cameraData);
        }
    }

    public initLight(light: Laya.DirectionLight) {
        let lightData = this._levelConfig["light"];
        if (lightData) {
            this.initNode(light, lightData);
        }
    }

    public Preload(urls: string[]) {
        urls.push(ResUrl.config_url());
        // 当前 关卡 初始化
        let preloadUrls = this.getSceneByLv(CommonSaveData.instance.levelId).scenePrefabUrl();
        urls.push.apply(urls, preloadUrls);
    }

    // 预加载场景资源
    public preloadSceneRes(lv: number) {
        this.getSceneByLv(lv).loadRes();
    }

    public gameWin() {
        MesManager.instance.event3d(EEventScene.GameWin);
    }

    public gameFail() {
        MesManager.instance.event3d(EEventScene.GameFail);
    }

    public getNowScene() {
        return this.getSceneByLv(CommonSaveData.instance.levelId);
    }

    public getSceneByLv(lv: number): Scene {
        let lvConfig = LevelConfig.data[lv];
        if (!lvConfig)
            console.error("不存在此关卡--", lv);
        let sceneName = lvConfig.sceneName;
        if (!this._scenes[sceneName]) {
            this._scenes[sceneName] = new Scene(this._levelConfig[sceneName]);
        }
        return this._scenes[sceneName];
    }

    public getLvResName(lv: number) {
        return this.getSceneByLv(lv).scenePrefabResName();
    }

    public getLvResUrl(lv: number) {
        return this.getSceneByLv(lv).scenePrefabUrl();
    }

    initNode(node: Laya.Sprite3D, data: node | Prefab) {
        node.name = data.name;
        node.transform.localPosition = this._parseVector3(data.position);
        node.transform.localRotationEuler = this._parseVector3(data.euler);
        node.transform.localScale = this._parseVector3(data.scale)
    }

    private _parseVector3(str: string): Laya.Vector3 {
        var strs = str.split(',');
        return new Laya.Vector3(Number(strs[0]), Number(strs[1]), Number(strs[2]));
    }
}